<?php
namespace app\center\controller;
use app\center\logic\DayAccountLogic;

/**
 * 日记账api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/31
 * Time: 16:28
 */
class DayAccount extends Base {

    public function __construct(){
        parent::__construct();
        $this->da_logic = new DayAccountLogic();
    }

    /**
     * 获取概述信息
     * @return \think\response\Json
     */
    public function ajax_get_overview(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $wra = new \app\common\controller\WorkRecordAccount();

        $maps = $this->da_logic->get_search_wrat_maps($params['department']);
        $list = model('user')->get_column($maps,'DISTINCT department','','','','','',false);

        $total_income_money = 0;
        $total_expend_money = 0;
        foreach($list as $k => $v){
            //获取日记账统计
            $total = $wra->get_total($v,$params['start_time'],$params['end_time']);
            $total_income_money += $total['income_money'];
            $total_expend_money += $total['expend_money'];
        }

        $data['total_income_money'] = $total_income_money;
        $data['total_expend_money'] = $total_expend_money;
        return json($this->da_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取日记账
     * @return \think\response\Json
     */
    public function ajax_get_da(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->da_logic->get_search_da_maps($params['department'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_account')->get_list($maps,'*','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //类型及收支
            $list[$k]['type_and_ie'] = $v['type'] . '-' . $v->income_and_expend_name;
            $list[$k]['money'] = deal_number_div_100($v['money']);

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['name'] = $v['department'] . '-' . $u_detail['username'];
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->da_logic->get_response(1,'OK',$data));
    }
}
